package cn.itsource.org.controller;

import cn.itsource.basic.query.BaseQuery;
import cn.itsource.basic.utils.AjaxResult;
import cn.itsource.org.domain.Department;
import cn.itsource.org.query.DepartmentQuery;
import cn.itsource.org.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 部门的控制层
 */
@RestController    // @Controller  + @ResponseBody联合注解
@RequestMapping("/dept")    // 暴露一级路径
@Api(value = "部门接口",tags = {"部门的接口数据"})
public class DepartmentController {

    @Autowired
    private IDepartmentService service;


    /**
     *查询所有
     * @return
     */
    @GetMapping("/getAll")
    @ApiOperation(value = "部门",notes = "查部门数据就用我！！")
    public AjaxResult getAll(){
        return AjaxResult.createSuccess(service.getAll());
    }


    /**
     *分页查询
     * @return
     */
    @PostMapping("/findPage")
    @ApiOperation(value = "分页",notes = "查询部门分页数据！！")
    public AjaxResult findPage(@ApiParam(value = "分页参数",required = true) @RequestBody DepartmentQuery query){
        return AjaxResult.createSuccess(service.getAllByPage(query));
    }


    /**.
     * 根据id删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            service.delete(id);
            return AjaxResult.createSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.createError("删除失败，有问题请联系曹杰！！");
        }
    }

    /**
     * 新增和修改的操作
     * @param department
     * @return
     *
     * @Valid：  表示对向前出入的形参做规则校验
     */
    @PostMapping("/saveOrUpdate")
    public AjaxResult saveOrUpdate(@Valid @RequestBody Department department){
        try {
            service.saveOrUpdate(department);
            return AjaxResult.createSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.createError("操作失败，有问题请联系曹杰！！");
        }
    }

    /**
     * 部门树的数据获取
     * @return
     */
    @GetMapping("/treeDept")
    public AjaxResult treeDept(){
        return AjaxResult.createSuccess(service.treeDept());
    }

    /**
     * 获取所有的父级部门数
     * @return
     */
    @GetMapping("/getParentDept")
    public AjaxResult getParentDept(){
        return AjaxResult.createSuccess(service.getParentDept());
    }

}
